package com.func.boom.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.func.boom.dao.dto.RolesDto;
import com.func.boom.dao.dto.SaveRolesDto;
import com.func.boom.entity.TResourceModule;
import com.func.boom.entity.TResourceRole;
import com.func.boom.entity.TSysUser;
import com.func.boom.enums.ResultEnums;
import com.func.boom.service.ModuleService;
import com.func.boom.service.RoleService;
import com.func.boom.service.UserService;
import com.func.boom.utils.ResultEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Yulei
 * @date 2020-09-28 13:47:16
 * @Description
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private ModuleService moduleService;
    @Resource
    private HttpServletRequest request;

    /**
     * 查询全部用户信息
     * @return
     */
    @RequestMapping("/getUserList")
    public ResultEntity<?> getUserList(@RequestBody TSysUser user){
        PageHelper.startPage(user.getPageNumber(), user.getPageSize());
        PageInfo pageInfo = new PageInfo(userService.getUserList(user));
        Map map = CollectionUtil.newHashMap();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return ResultEntity.success(ResultEnums.SUCCESS_QUERY, map);
    }

    /**
     * 获取用户权限并排除app
     * @return
     */
    @PostMapping("/userRole")
    public ResultEntity<?> getUserRole(@RequestBody RolesDto rolesDto){
        //根据用户id获取对应权限
        QueryWrapper<TResourceRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", rolesDto.getUserId());
        TResourceRole tResourceRole = roleService.getOne(queryWrapper);
        String[] roleArr = tResourceRole.getModules().split(",");
        List<String> roleList = CollectionUtil.toList(roleArr);
        //获取module对应的app数据
        QueryWrapper<TResourceModule> qw = new QueryWrapper<>();
        qw.in("function_type", 1, 3);
        List<TResourceModule> moduleList = moduleService.list(qw);
        List<String> moduleIds = CollUtil.getFieldValues(moduleList, "id", String.class);
        //过滤掉app
        List<String> list = roleList.stream().filter(item -> {
            return !CollUtil.contains(moduleIds, item);
        }).collect(Collectors.toList());

        return ResultEntity.success(ResultEnums.SUCCESS_QUERY, list);
    }

    /**
     * 获取用户权限并排除app
     * @return
     */
    @PostMapping("/saveRole")
    public ResultEntity<?> saveRole(@RequestBody SaveRolesDto rolesDto){
        //根据用户id获取对应权限
        QueryWrapper<TResourceRole> qw = new QueryWrapper<>();
        qw.in("user_id", rolesDto.getUserId());
        TResourceRole resourceRole = roleService.getOne(qw);
        if (resourceRole == null) {
            resourceRole = new TResourceRole();
            resourceRole.setUserId(rolesDto.getUserId());
            resourceRole.setCreateTime(new Date());
        } else {
            resourceRole.setUpdateTime(new Date());
        }
        resourceRole.setModules(rolesDto.getModules());
        roleService.saveOrUpdate(resourceRole);

        return ResultEntity.success(ResultEnums.SUCCESS_SAVE);
    }

}
